<template>
  <el-container class="home-contain">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="IsCollapse ? '64px' : '200px'">
        <!-- 展开/收起菜单 -->
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 一级导航 -->
        <!-- 
        unique-opened 是否只保持一个子菜单的展开
        collapse是否水平折叠收起菜单（仅在 mode 为 vertical 时可用）
        collapse-transition 是否开启折叠动画 
        router 是否使用 vue-router 的模式，启用该模式会在激活导航时以 index 作为 path 进行路由跳转
        default-active="/users"   等同于:default-active="$route.path"
        -->
        <el-menu
          background-color="#323744"
          text-color="#fff"
          active-text-color="#4D92DC"
          unique-opened
          :collapse="IsCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <el-submenu
            :index="item.id + ''"
            v-for="item in menuList"
            :key="item.id"
          >
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 标题 -->
              <span> {{ item.authName }}</span>
            </template>

            <!-- 二级导航 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavStatus('/' + subItem.path)"
            >
              <!-- 图标 -->
              <i class="el-icon-menu"></i>
              <!-- 标题 -->
              <span>{{ subItem.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体内容 -->
      <el-main>
        <!-- 路由占位符 展示welcome组件-->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    // 保存左侧菜单数据
    return {
      menuList: [],
      // 一级菜单icon
      iconsObj: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        102: 'iconfont icon-shangpin',
        101: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao',
      },
      // 侧边栏是否展开属性
      IsCollapse: false,
      // 动态获取请求路径
      activePath: '',
    }
  },
  // 生命周期函数,获取左侧菜单导航数据
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    // 退出按钮事件：清空tocken，跳转到登陆页面
    logout() {
      window.sessionStorage.removeItem('tocken')
      this.$router.push('/login')
    },
    // 获取左侧导航栏数据
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      console.log(res)

      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
    },
    // 展开或收起左侧菜单
    toggleCollapse() {
      this.IsCollapse = !this.IsCollapse
    },
    saveNavStatus(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath //再次切换二级菜单 要重新赋值一下地址
    },
  },
}
</script>

<style lang="less" scoped>
.home-contain {
  height: 100%;
}
.el-header {
  background-color: #373c41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 20px;
    }
  }
}
.el-aside {
  background-color: #323744;
  .el-menu {
    border-right: none;
  }
}

.el-main {
  background-color: #eaedf2;
}

.toggle-button {
  background-color: #475163;
  color: #fff;
  font-size: 10px;
  line-height: 24px;
  text-align: center;
  cursor: pointer;
  letter-spacing: 0.2rem;
}
</style>